package com.pj.wisdomlogistics.ui.activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.pj.wisdomlogistics.MyApplication;
import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.cons.Cons;
import com.pj.wisdomlogistics.entity.MeetingSimpleInfo;
import com.pj.wisdomlogistics.mvp.contract.ApplyMeetingContract;
import com.pj.wisdomlogistics.mvp.frame.MvpActivity;
import com.pj.wisdomlogistics.mvp.frame.base.BaseView;
import com.pj.wisdomlogistics.mvp.presenter.ApplyMeetingPresenter;
import com.pj.wisdomlogistics.other.EventCenter;
import com.pj.wisdomlogistics.ui.view.AutoRadioGroup;
import com.pj.wisdomlogistics.utils.ActivityManagerUtil;
import com.pj.wisdomlogistics.utils.CalendarUtil;
import com.pj.wisdomlogistics.utils.CustomToast;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import butterknife.BindView;
import butterknife.OnClick;

public class ApplyMeetingActivity extends MvpActivity<ApplyMeetingPresenter> implements ApplyMeetingContract.View {

    @BindView(R.id.tv_apply_dept)
    TextView mTvApplyDept;
    @BindView(R.id.tv_apply_dept_contacts)
    TextView mTvApplyDeptContacts;
    @BindView(R.id.tv_apply_dept_call)
    TextView mTvApplyDeptCall;
    @BindView(R.id.tv_apply_dept_phone)
    TextView mTvApplyDeptPhone;
    @BindView(R.id.tv_meeting_date)
    TextView mTvMeetingDate;
    @BindView(R.id.tv_meeting_starttime)
    TextView mTvMeetingStarttime;
    @BindView(R.id.et_meeting_content)
    EditText mEtMeetingContent;
    @BindView(R.id.rb_big_schedule_on)
    RadioButton mRbBigScheduleOn;
    @BindView(R.id.rb_big_schedule_off)
    RadioButton mRbBigScheduleOff;
    @BindView(R.id.rg_big_schedule)
    AutoRadioGroup mRgBigSchedule;
    @BindView(R.id.et_meeting_count)
    EditText mEtMeetingCount;
    @BindView(R.id.et_rostrum_count)
    EditText mEtRostrumCount;
    @BindView(R.id.et_voice_normal)
    EditText mEtVoiceNormal;
    @BindView(R.id.et_voice_stand)
    EditText mEtVoiceStand;
    @BindView(R.id.rg_music)
    RadioGroup mRadioGroup;
    @BindView(R.id.rb_music_on)
    RadioButton mRbMusicOn;
    @BindView(R.id.rb_music_off)
    RadioButton mRbMusicOff;
    @BindView(R.id.et_banner_content)
    EditText mEtBannerContent;
    @BindView(R.id.et_meeting_other)
    EditText mEtMeetingOther;
    @BindView(R.id.rb_lift_top)
    CheckBox mCKLiftTop;
    @BindView(R.id.rb_lift_down)
    CheckBox mCKLiftDown;
    @BindView(R.id.tv_meeting_open_time)
    TextView mTvMeetingOpenTime;

    @BindView(R.id.rb_place_zf)
    RadioButton mRbPlaceZf;
    @BindView(R.id.rb_place_shy)
    RadioButton mRbPlaceShy;

    private int meetingId;

    private int meetingCount;//会议室可容纳人数
    private int rostrumCount;//主席台人数
    private int mikeCount;//普通话筒数量
    private int standCount;//立式话筒数量
    private MeetingSimpleInfo mInfo;


    @Override
    protected BaseView getBaseView() {
        return this;
    }

    @Override
    public void initView() {
        ultimateBar.setColorBar(ContextCompat.getColor(this, R.color.text_blue));

        mInfo = (MeetingSimpleInfo) getIntent().getSerializableExtra(Cons.MeetingInfo.MEETING_COUNT);
        meetingCount = mInfo.getMeetingContent();
        rostrumCount = mInfo.getRostrumCount();
        mikeCount = mInfo.getMikeCount();
        standCount = mInfo.getStandMike();

        mTvApplyDept.setText(MyApplication.user.getDeptName());
        mTvApplyDeptContacts.setText(MyApplication.user.getName());
        mTvApplyDeptCall.setText(MyApplication.user.getTelphone());
        mTvApplyDeptPhone.setText(MyApplication.user.getMobile());

        meetingId = getIntent().getIntExtra(Cons.MeetingInfo.MEETING_SELECT_ID, -1);


    }

    @Override
    public void initData() {

    }

    @Override
    public int getContentView() {
        return R.layout.activity_apply_meeting;
    }

    @Override
    public boolean isApplyEventBus() {
        return false;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {
    }


    @OnClick({R.id.iv_back, R.id.tv_send, R.id.tv_meeting_date, R.id.tv_meeting_starttime,R.id.tv_meeting_open_time , R.id.iv_remark})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                ActivityManagerUtil.create().finishActivity();
                break;
            case R.id.tv_send:
                String meetingDate = mTvMeetingDate.getText().toString().trim();
                String meetingStartTime = mTvMeetingStarttime.getText().toString().trim();
                String meetingContent = mEtMeetingContent.getText().toString().trim();
                int isBigScheduleOn = mRbBigScheduleOn.isChecked() ? 1 : 0;
                String isLift = mCKLiftTop.isChecked() ? "高区" : "" + (mCKLiftDown.isChecked() ? "低区" : "");
                String place = mRbPlaceZf.isChecked() ? "1" : "2";
                String meetingCount = mEtMeetingCount.getText().toString().trim();
                String rostrumCount = mEtRostrumCount.getText().toString().trim();
                String voiceNormal = mEtVoiceNormal.getText().toString().trim();
                String voiceStand = mEtVoiceStand.getText().toString().trim();
                if(TextUtils.isEmpty(voiceStand)){
                    mEtVoiceStand.setText("0");
                    voiceStand = "0";
                }
                int music = mRadioGroup.getCheckedRadioButtonId() == R.id.rb_music_on ? 1 : 0;
                String banner = mEtBannerContent.getText().toString().trim();
                String other = mEtMeetingOther.getText().toString().trim();
                String openTime = mTvMeetingOpenTime.getText().toString().trim();

                boolean isOk = checkInfo(meetingDate, meetingStartTime, meetingContent,
                        meetingCount, rostrumCount, voiceNormal, voiceStand, banner, other , openTime);

                if (!isOk) return;

                mPresenter.applyMeeting(
                        MyApplication.userID ,
                        MyApplication.user.getName() ,
                        MyApplication.user.getTelphone() ,
                        MyApplication.user.getMobile() ,
                        meetingDate ,
                        meetingStartTime ,
                        "" ,
                        meetingContent ,
                        meetingId ,
                        isBigScheduleOn ,
                        meetingCount
                        ,rostrumCount ,
                        Integer.parseInt(voiceNormal) ,
                        voiceStand ,
                        music ,
                        banner ,
                        other ,
                        isLift ,
                        openTime ,
                        place);

                showProgressDialog();
                break;
            case R.id.tv_meeting_date:
                CalendarUtil.create().setCalendarDate(this, mTvMeetingDate);
                break;
            case R.id.tv_meeting_starttime:
                CalendarUtil.create().setCalendarTime(this, mTvMeetingStarttime);
                break;
            case R.id.tv_meeting_open_time:
                CalendarUtil.create().setCalendarTime(this , mTvMeetingOpenTime);
                break;
            case R.id.iv_remark:
                ImageView mImageView = new ImageView(this);
                mImageView.setImageResource(R.drawable.note1);
                mImageView.setScaleType(ImageView.ScaleType.FIT_XY);

                AlertDialog.Builder  builder  =  new AlertDialog.Builder(this);
                AlertDialog ad = builder.create();
                ad.setCanceledOnTouchOutside(true);
                ad.setView(mImageView);
                ad.show();
                break;
        }
    }

    @Override
    public void onError(String errMsg) {
        dismissProgressDialog();
        CustomToast.showToast(this , errMsg , 1000);
    }

    @Override
    public void onSuccess(int id) {
        dismissProgressDialog();
        Intent mIntent = new Intent(this , SuccessActivity.class);
        mIntent.putExtra(Cons.MeetingInfo.MEETING_ID, id);
        startActivity(mIntent);
        ActivityManagerUtil.create().finishActivity();
    }

    private boolean checkInfo(
            String meetingDate,
            String meetingStartTime,
            String meetingContent,
            String meetingCo,
            String rostrumCo,
            String voiceNormal,
            String voiceStand,
            String banner,
            String other ,
            String meetingOpenTime) {


        if (TextUtils.isEmpty(meetingDate) ||
                TextUtils.isEmpty(meetingStartTime) ||
                TextUtils.isEmpty(meetingContent) ||
                TextUtils.isEmpty(meetingCo) ||
                TextUtils.isEmpty(rostrumCo) ||
                TextUtils.isEmpty(voiceNormal) ||
                TextUtils.isEmpty(meetingOpenTime)) {
            CustomToast.showToast(this , "请将信息填写完成" , 1000);
            return false;
        }

//        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        try {
//            Date mDate = mSimpleDateFormat.parse(meetingDate);
//            Date mDateNow = new Date(System.currentTimeMillis());
//            if(getNextDay(mDateNow , 1).after(mDate)){
//                CustomToast.showToast(this , "必须提前两天申请会议室" , 1000);
//                return false;
//            }
//
//        } catch (ParseException mE) {
//            mE.printStackTrace();
//        }

        SimpleDateFormat mSimpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String startTime = meetingDate + " " + meetingStartTime;
        String openTime = meetingDate + "" + meetingOpenTime;
        try {
            Date openDate = mSimpleDateFormat1.parse(openTime);
            if(openDate.after(mSimpleDateFormat1.parse(startTime))){
                CustomToast.showToast(this , "请选择合适的开门时间" , 1000);
                return false;
            }
        } catch (ParseException mE) {
            mE.printStackTrace();
        }

        if(Integer.parseInt(rostrumCo) > rostrumCount){
            CustomToast.showToast(this , "该会议室主席台人数上限为" + rostrumCount + "人" , 1000);
            return false;
        }

        int totalCount = Integer.parseInt(meetingCo) + Integer.parseInt(rostrumCo);
        if(totalCount > meetingCount){
            CustomToast.showToast(this , "该会议室最多容纳" + meetingCount + "人" , 1000);
            return false;
        }

        int voiceNormalCount = Integer.parseInt(voiceNormal);
        int voiceStandCount = Integer.parseInt(voiceStand);
        if(voiceNormalCount > mikeCount){
            CustomToast.showToast(this , "该会议室最多放置" + mikeCount + "个普通话筒" , 1000);
            return false;
        }

        if(voiceStandCount > standCount){
            if(standCount == 0){
                CustomToast.showToast(this , "该会议室无法安放立式话筒" , 1000);
                return false;
            }
            CustomToast.showToast(this , "该会议室最多放置" + standCount + "立式话筒" , 1000);
            return false;
        }

        return true;

    }

    //获取下一天日期
    private Date getNextDay(Date date , int index) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, index);
        date = calendar.getTime();
        return date;
    }

}
